package com.college.products.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.common.pojo.ao.Cart;
import com.college.common.pojo.dto.products.AddCartDTO;
import com.college.common.pojo.dto.products.QueryCartDTO;
import com.college.common.pojo.dto.products.UpdateCartDTO;
import com.college.common.pojo.model.page.PageParamRequest;
import com.college.common.response.CommonResult;
import com.college.common.utils.object.ObjectUtil;
import com.college.products.mapper.CartMapper;
import com.college.products.service.CartService;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车 服务实现类
 * @author 小C工作室
 * @since 2025-02-22
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<Cart> queryList(QueryCartDTO queryCartDTO) {
        return cartMapper.queryList(queryCartDTO);
    }

    @Override
    public List<Cart> queryListPage(QueryCartDTO queryCartDTO, PageParamRequest pageParamRequest) {
        PageMethod.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return cartMapper.queryList(queryCartDTO);
    }

    @Override
    public CommonResult<String> addCart(AddCartDTO addCartDTO) {
        return this.save(addCartDTO)? CommonResult.success("购物车新增成功") : CommonResult.failed("购物车新增失败");
    }

    @Override
    public CommonResult<String> updateCart(UpdateCartDTO updateCartDTO) {
        Cart cart = ObjectUtil.objectCopyProperties(updateCartDTO, Cart.class);
        return this.updateById(cart) ? CommonResult.success("购物车修改成功") : CommonResult.failed("购物车修改失败");
    }
}
